﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using GSQ.BusinessEntities;
using GSQ.DataAccess.DataSetTableAdapters;

namespace GSQ.DataAccess
{
    public class VersionesDA
    {
        private VERSIONESTableAdapter Adapter = new VERSIONESTableAdapter();
        private DataSet.VERSIONESDataTable entities;
        private DataSet.VERSIONESRow entity;

        public decimal Insert(VERSIONES value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    Adapter.Connection = cnn;

                    entities = new DataSet.VERSIONESDataTable();

                    entity = entities.NewVERSIONESRow();

                    entity.ID = value.ID;
                    entity.VERSION = value.VERSION.Trim();
                    entity.ACTIVO = value.ACTIVO;

                    entities.AddVERSIONESRow(entity);
                    Adapter.Update(entities);

                    value.ID = entity.ID;
                    return value.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public decimal Update(VERSIONES value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    entities = Adapter.GetDataByID(value.ID);
                    entity = entities.FirstOrDefault();


                    entity.VERSION = value.VERSION.Trim();
                    entity.ACTIVO = value.ACTIVO;

                    this.Adapter.Update(entities);

                    return entity.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public VERSIONES GetById(decimal id)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    VERSIONES version;

                    entity = this.Adapter.GetDataByID(id).FirstOrDefault();

                    if (entity != null)
                    {
                        version = new VERSIONES
                            {
                                ID = entity.ID,
                                VERSION = entity.VERSION,
                                ACTIVO = entity.ACTIVO
                            };
                    }
                    else
                        version = null;

                    return version;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<VERSIONES> GetAll()
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<VERSIONES> lst = new List<VERSIONES>();
                    foreach (DataSet.VERSIONESRow rw in this.Adapter.GetData())
                    {
                        lst.Add(new VERSIONES
                        {
                            ID = rw.ID,
                            VERSION = rw.VERSION,
                            ACTIVO = rw.ACTIVO
                        });
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<VERSIONES> GetByDescripcion(string value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<VERSIONES> lst = new List<VERSIONES>();
                    foreach (DataSet.VERSIONESRow rw in this.Adapter.GetDataByVersion(value.Trim()))
                    {
                        lst.Add(new VERSIONES
                        {
                            ID = rw.ID,
                            VERSION = rw.VERSION,
                            ACTIVO = rw.ACTIVO
                        });
                    }
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }
    }
}
